package xyz.riceball.common.service.impl;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.riceball.common.entity.dto.SysLogDTO;
import xyz.riceball.common.entity.po.SysLogPO;
import xyz.riceball.common.mapper.SysLogMapper;
import xyz.riceball.common.service.SysLogService;
import xyz.riceball.framework.core.context.UserContext;
import xyz.riceball.framework.core.context.WebContextHolder;
import xyz.riceball.framework.core.enums.ENMethodType;
import xyz.riceball.framework.core.util.HttpUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * 系统日志查看 Service实现层
 *
 * @author xiaovcloud
 * @since 2022-07-26 19:18:41
 */
@Service("sysLogService")
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLogPO> implements SysLogService {

    @Override
    public Page<SysLogPO> selectPage(Page<SysLogPO> page, SysLogDTO sysLogDTO) {
        LambdaQueryWrapper<SysLogPO> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotEmpty(sysLogDTO.getOperator())) {
            wrapper.eq(SysLogPO::getOperator, sysLogDTO.getOperator());
        }
        if (ObjectUtil.isNotEmpty(sysLogDTO.getLevel())) {
            wrapper.eq(SysLogPO::getLevel, sysLogDTO.getLevel());
        }
        if (StrUtil.isNotEmpty(sysLogDTO.getUri())) {
            wrapper.eq(SysLogPO::getUri, sysLogDTO.getUri());
        }
        if (ObjectUtil.isNotEmpty(sysLogDTO.getOperatorTimeFrom())) {
            wrapper.ge(SysLogPO::getOperatorTime, sysLogDTO.getOperatorTimeFrom());
        }
        if (ObjectUtil.isNotEmpty(sysLogDTO.getOperatorTimeTo())) {
            wrapper.le(SysLogPO::getOperatorTime, sysLogDTO.getOperatorTimeTo());
        }
        wrapper.orderByDesc(SysLogPO::getOperatorTime);
        return page(page, wrapper);
    }

    @Override
    public boolean saveErrorSysLog(SysLogPO sysLogPO, HttpServletRequest request, Throwable e) {
        fillLog(sysLogPO);
        sysLogPO.setLevel(2);
        sysLogPO.setMsg(ExceptionUtil.getMessage(e));
        String stackMsg = ExceptionUtil.stacktraceToString(e);
        sysLogPO.setErrorStack(stackMsg);
        return getBaseMapper().insert(sysLogPO) == 1;
    }

    @Override
    public boolean saveInfoSysLog(SysLogPO sysLogPO, HttpServletRequest request) {
        fillLog(sysLogPO);
        sysLogPO.setLevel(1);
        return getBaseMapper().insert(sysLogPO) == 1;
    }

    /**
     * 填充基础信息
     *
     * @return
     */
    private SysLogPO fillLog(SysLogPO sysLogPO) {
        HttpServletRequest httpServletRequest = WebContextHolder.getHttpServletRequest();
        String osName = HttpUtils.getOsName(httpServletRequest);
        sysLogPO.setOs(osName);
        String browserName = HttpUtils.getBrowserName(httpServletRequest);
        sysLogPO.setBrowser(browserName);
        sysLogPO.setIp(WebContextHolder.getIp());
        sysLogPO.setOperator(UserContext.getUserId());
        String methodType = httpServletRequest.getMethod();
        sysLogPO.setMethodType(ENMethodType.valueOf(methodType).getValue());
        sysLogPO.setUri(WebContextHolder.getRequestURI());
        sysLogPO.setOperatorTime(LocalDateTime.now());
        return sysLogPO;
    }
}